package com.xiaotu.spider.baidu.pageprocessor;

import com.xiaotu.common.model.StarAwardRecordModel;
import com.xiaotu.common.model.TVAwardRecordModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.spider.AUrlPageProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 TVAwardBaikePageProcessor
 * @日期 2021年2月8日
 * @作者 王艳龙
 * @功能 百度百科作品获奖记录
 */
@Component("TVAwardBaikePageProcessor")
public class TVAwardBaikePageProcessor extends AUrlPageProcessor
{
    private String tv_url = "https://www.baidu.com/";
	
    @Autowired
	private SupplementGuDuoRecordService starService;
    
    @Override
	protected String[] getStartRequestUrls() {
    	return new String[]{tv_url};
	}
	
    @Override
   	public void process(Page page) {
    	if (Constants.PageDataType.STAR_RECORD.equals(this.getPageDataType(page)))
            this.tvRecord(page);
        else
            this.pageProcess(page);
    }
    
    private void pageProcess(Page page) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("rows",5000);
		param.put("page",9);
    	List<Map<String,Object>> starList = starService.getAllTvInfoList(param);
    	for (Map<String, Object> starInfoMap : starList) {
			String linkUrl = "https://baike.baidu.com/item/"+ KeywordEncode.encode(starInfoMap.get("tvname")+"")+"?" +
					"tvid="+starInfoMap.get("tvid");
			PageUtils.addTargetRequest(page, linkUrl, page.getRequest().getExtras());
    	}
    }
    
	private void tvRecord(Page page) {
		String linkUrl = KeywordEncode.decode(page.getUrl().toString());
		String tvName = null;
		String tvId = null;
		//String resActor=null;
		//String resDirector = null;
		tvName = linkUrl.substring(linkUrl.indexOf("item/")+5, linkUrl.indexOf("?tvid"));
		tvId = linkUrl.substring(linkUrl.indexOf("tvid=")+5, linkUrl.length());
		List<String> awardRecordList = page.getHtml().xpath("//div[@class='para-title level-2']//h2[@class='title-text']").all();
		//如果样式不满足   判断是否有获奖记录  0：无 1：有
		int status =0;
		for (String isAwardRecord:awardRecordList){
			if(isAwardRecord.contains("获奖")){
				status=1;
				break;
			}else{
				status=0;
			}
		}
		page.putField(TVAwardRecordModel.class.getName(),new TVAwardRecordModel(tvId,tvName,status));
		/*resActor = linkUrl.substring(linkUrl.indexOf("actor=")+6, linkUrl.indexOf("&director"));
		resDirector = linkUrl.substring(linkUrl.indexOf("director=")+9, linkUrl.length());
		List<String> nameList = page.getHtml().xpath("//dt[@class='basicInfo-item name']").all();
		List<String> valueList = page.getHtml().xpath("//dd[@class='basicInfo-item value']").all();
		String name = null, actor=null,director=null;
		boolean actorflag = false;
		boolean directorflag = false;
		if (nameList != null && !nameList.isEmpty() && nameList.size() == valueList.size()){
			for (int j = 0; j < nameList.size(); j++) {
				if(nameList.get(j).contains("中文名")){
					name = valueList.get(j);
				}
				if(nameList.get(j).contains("主") && nameList.get(j).contains("演")){
					actor = valueList.get(j);
				}
				if(nameList.get(j).contains("导") && nameList.get(j).contains("演")){
					director = valueList.get(j);
				}
			}*/

			/*if(StringUtil.isNotEmpty(resActor)){
				String[] actorArr = resActor.split(",");
				for (String actorStr : actorArr) {
					if(StringUtil.isNotEmpty(actor)){
						if(actor.contains(actorStr)){
							actorflag = true;
							break;
						}
					}
				}
			}
			if(StringUtil.isNotEmpty(resDirector)){
				String[] directorArr = resDirector.split(",");
				for (String directorStr : directorArr) {
					if(StringUtil.isNotEmpty(director)){
						if(director.contains(directorStr)){
							directorflag = true;
							break;
						}
					}
				}
			}

			if(StringUtil.isNotEmpty(resDirector)){
				if(actorflag && directorflag){
					List<String> awardRecordList = page.getHtml().xpath("//h2[@class='title-text']").all();
					//如果样式不满足   判断是否有获奖记录  0：无 1：有
					int status =0;
					for (String isAwardRecord:awardRecordList){
						if(isAwardRecord.contains("获奖记录") || isAwardRecord.contains("所获奖项")){
							status=1;
							break;
						}else{
							status=0;
						}
					}
					page.putField(TVAwardRecordModel.class.getName(),new TVAwardRecordModel(tvId,tvName,status));
				}
			}else{
				if(actorflag){
					List<String> awardRecordList = page.getHtml().xpath("//h2[@class='title-text']").all();
					//如果样式不满足   判断是否有获奖记录  0：无 1：有
					int status =0;
					for (String isAwardRecord:awardRecordList){
						if(isAwardRecord.contains("获奖记录") || isAwardRecord.contains("所获奖项")){
							status=1;
							break;
						}else{
							status=0;
						}
					}
					page.putField(TVAwardRecordModel.class.getName(),new TVAwardRecordModel(tvId,tvName,status));
				}else{
					List<String> awardRecordList = page.getHtml().xpath("//h2[@class='title-text']").all();
					//如果样式不满足   判断是否有获奖记录  0：无 1：有
					int status =0;
					for (String isAwardRecord:awardRecordList){
						if(isAwardRecord.contains("获奖记录") || isAwardRecord.contains("所获奖项")){
							status=1;
							break;
						}else{
							status=0;
						}
					}
					page.putField(TVAwardRecordModel.class.getName(),new TVAwardRecordModel(tvId,tvName,status));
				}
			}*/
				/*boolean isAward=true;
				List<String> titleList =  page.getHtml().xpath("//table[@data-sort='sortDisabled']/tbody").all();
				for (String titleStr : titleList) {
					Html titleHtml = new Html(titleStr);
					String titleContent = titleHtml.xpath("//tr[1]/th/text()").get().trim();
					if(titleContent.equals(title1)){
						List<String> title1List = titleHtml.xpath("//tr").all();
						for (String bearRecord1 : title1List) {
							if (bearRecord1.equals(title1List.get(0))) {
								continue;
							}
							Html bearRecord1Html = new Html(bearRecord1);
							List<String> bearRecord1List = bearRecord1Html.xpath("//td").all();
							for (String bearcontent : bearRecord1List) {

							}

						}
						isAward =false;

					}else if(titleContent.equals(title2)){
						isAward =false;
					}else if(titleContent.equals(title3)){
						isAward =false;
					}
				}

				if(isAward){
					List<String> awardRecordList = page.getHtml().xpath("//h2[@class='title-text']").all();
					for (String isAwardRecord:awardRecordList){
						//如果样式不满足   判断是否有获奖记录  0：无 1：有
						int status =0;
						if(isAwardRecord.contains("获奖记录")){
							status=1;
						}else{
							status=0;
						}
						page.putField(TVAwardRecordModel.class.getName(),new TVAwardRecordModel(tvId,status));
					}
				}
		}*/
    }
    
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.TV_AWARD_BAIKE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("item"))
        	return Constants.PageDataType.STAR_RECORD;
		return Constants.PageDataType.SEARCH_LIST;
	}
}
